﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;

using Obsidian.Runtime;
using Obsidian.Edm;
using Oak.Base;
using Oak.Model;
using Oak.Core.Data;

namespace Oak.Core.Logic
{
    public class ActionLG : OakLG<OakSession>
    {

        private static bool inited = false;
        private static Hashtable htActionsById = Hashtable.Synchronized(new Hashtable());
        private static Hashtable htActionsByName = Hashtable.Synchronized(new Hashtable());

        public ActionLG(OakSession session) : base(session) { }


        /// <summary>
        /// 获得接口信息列表
        /// </summary>
        /// <param name="qry">查询信息</param>
        /// <param name="listAttr">列表属性</param>
        /// <param name="msg">返回信息</param>
        /// <returns></returns>
        public List<CoActionInfo> GetActionList(CoActionQry qry, out ListAttrInfo listAttr, out string msg)
        {
            if (!qry.CheckPagingAttrs(40, 100, out msg))
            {
                listAttr = null;
                return null;
            }

            ActionDA actionDA = new ActionDA();
            List<CoActionInfo> list;
            try
            {
                list = actionDA.SelectList(qry, out listAttr);
            }
            catch (Exception ex)
            {
                string code = Logger.Error(ex);
                msg = "发生系统错误:" + code;
                listAttr = null;
                return null;
            }

            msg = "";
            return list;
        }


        /// <summary>
        /// 添加接口信息
        /// </summary>
        /// <param name="action">接口信息</param>
        /// <param name="msg">返回信息</param>
        /// <returns></returns>
        public CoActionInfo AddAction(CoActionInfo action, out string msg)
        {

            if (!VerifyAction(action, out msg))
                return null;

            action.Modified.Now();
            action.Created.Now();

            try
            {
                action.Save();
            }
            catch (Exception ex)
            {
                string code = Logger.Error(ex);
                msg = "发生系统错误：" + code;
                return null;
            }

            if (!LoadActionsCache(out msg))
                return null;

            msg = "";
            return action;
        }

        /// <summary>
        /// 更新接口信息
        /// </summary>
        /// <param name="action">接口信息</param>
        /// <param name="msg">返回信息</param>
        /// <returns></returns>
        public CoActionInfo UpdateAction(CoActionInfo action, out string msg)
        {

            if (action.ActionId.IsNull)
            {
                msg = "接口Id不能为空";
                return null;
            }

            long actionId = action.ActionId.Value;

            CoActionInfo dbAct = this.GetActionById(actionId, out msg);
            if (dbAct == null)
                return null;

            dbAct.ResetAssigned();

            if (!action.ActionName.IsNull)
                dbAct.ActionName.Set(action.ActionName.Value);

            if (!action.ActionCatId.IsNull)
                dbAct.ActionCatId.Set(action.ActionCatId.Value);

            if (!action.Description.IsNull)
                dbAct.Description.Set(action.Description.Value);

            if (!VerifyAction(dbAct, out msg))
                return null;

            dbAct.Modified.Now();

            try
            {
                dbAct.Update();
            }
            catch (Exception ex)
            {
                string code = Logger.Error(ex);
                msg = "发生系统错误：" + code;
                return null;
            }

            if (!LoadActionsCache(out msg))
                return null;

            msg = "";
            return dbAct;

        }

        /// <summary>
        /// 根据Id获得接口信息
        /// </summary>
        /// <param name="actionId">接口Id</param>
        /// <param name="msg">返回信息</param>
        /// <returns></returns>
        public CoActionInfo GetActionById(long actionId, out string msg)
        {
            if (!inited)
            {
                if (!LoadActionsCache(out msg))
                    return null;
                inited = true;
            }

            if (!htActionsById.ContainsKey(actionId))
            {
                msg = "该接口信息不存在";
                return null;
            }

            CoActionInfo action = (CoActionInfo)htActionsById[actionId];
            msg = "";
            return action;
        }

        /// <summary>
        /// 根据名称获得接口信息
        /// </summary>
        /// <param name="actionName">接口名称</param>
        /// <param name="msg">返回信息</param>
        /// <returns></returns>
        public CoActionInfo GetActionByName(string actionName, out string msg)
        {
            if (!inited)
            {
                if (!LoadActionsCache(out msg))
                    return null;
                inited = true;
            }

            if (!htActionsByName.ContainsKey(actionName))
            {
                msg = "该接口信息不存在";
                return null;
            }
            CoActionInfo action = (CoActionInfo)htActionsByName[actionName];
            msg = "";
            return action;
        }


        /// <summary>
        /// 删除接口信息
        /// </summary>
        /// <param name="actionId">接口Id</param>
        /// <param name="msg">返回信息</param>
        /// <returns></returns>
        public bool DeleteAction(long actionId, out string msg)
        {

            CoActionInfo dbAct = this.GetActionById(actionId, out msg);

            if (dbAct == null)
                return false;

            try
            {
                dbAct.Delete();
            }
            catch (Exception ex)
            {
                string code = Logger.Error(ex);
                msg = "发生系统错误：" + code;
                return false;
            }

            if (!LoadActionsCache(out msg))
                return false;

            msg = "";
            return true;

        }

        private bool LoadActionsCache(out string msg)
        {
            ActionDA actionDA = new ActionDA();
            List<CoActionInfo> list;
            try
            {
                list = actionDA.SelectList();
            }
            catch (Exception ex)
            {
                string code = Logger.Error(ex);
                msg = "发生系统错误:" + code;
                return false;
            }
            htActionsById.Clear();
            htActionsByName.Clear();
            foreach (CoActionInfo action in list)
            {
                htActionsById.Add(action.ActionId.Value, action);
                htActionsByName.Add(action.ActionName.Value, action);
            }

            msg = "";
            return true;
        }

        private bool VerifyAction(CoActionInfo action, out string msg)
        {
            if (action.ActionName.IsNullOrWhiteSpace)
            {
                msg = "接口名称不能为空";
                return false;
            }

            msg = "";
            return true;
        }

    }
}
